/*
 * Copyright 2011 Christoph Ludwig (christoph[at]chludwig.de). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.chludwig.ehumanities.provenance.provider;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.Provider;

import org.apache.abdera.model.Document;
import org.apache.abdera.model.Element;
import org.apache.abdera.parser.Parser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.chludwig.ehumanities.provenance.Atom.AbderaInstanceFactory;
import de.chludwig.ehumanities.provenance.exception.BrokenConfigurationException;

/**
 * Infrastructure for serializing and de-serializing both Atom feeds and Atom entries as JSON.
 * <p>
 * The implementation follows closely the <a href="http://blogs.oracle.com/sandoz/entry/jersey_and_abdera_with_a">example</a>
 * in <a href="http://blogs.oracle.com/sandoz/">Paul Sandoz's blog</a> at Oracle.</p>
 * <p>
 * <b>Note:</b> As of release 1.1.2, Abdera does not come with a JSON parser. Unless a custom 
 * parser was configured, reading JSON objects will always fail.</p>  
 */
@Provider
@Produces("application/json")
@Consumes("application/json")
public class AbderaJSONProvider extends AbderaProviderBase {

  private static final Logger LOG = LoggerFactory.getLogger(AbderaJSONProvider.class);
  
  /* (non-Javadoc)
   * @see de.chludwig.ehumanities.provenance.provider.AbderaProviderBase#read(java.io.InputStream)
   */
  @Override
  protected Document<Element> read(InputStream entityStream) {
    Parser parser = getJSONParser();
    return parser.parse(entityStream);   
  }

  /**
   * Obtains a parser for JSON serialized Abdera documents.
   * <p>
   * <b>Note:</b> As of release 1.1.2, Abdera does not come with a JSON parser. This method will 
   * therefore always fail.</p>
   * 
   * @return A parser for JSON serialized Abdera documents.
   * @throws BrokenConfigurationException Abdera failed to find a suitable parser implementation.
   */
  private Parser getJSONParser() {
    Parser parser;
    Exception cause = null;
    try {
      parser = (Parser)AbderaInstanceFactory.getInstance().getParserFactory().getParser("json");
    }
    catch(Exception ex) {
      LOG.error("Creation of a JSON parser failed.", ex);
      cause = ex;
      parser = null;
    }
    if(parser == null) {
      LOG.error("Cannot instantiate an Abdera JSON parser.");
      throw new BrokenConfigurationException("No Abdera JSON parser configured.", cause);
    }
    return parser;
  }

  /* (non-Javadoc)
   * @see de.chludwig.ehumanities.provenance.provider.AbderaProviderBase#write(org.apache.abdera.model.Document, java.io.OutputStream)
   */
  @Override
  protected void write(Document<Element> document, OutputStream entityStream) throws IOException {
    document.writeTo("json", entityStream);
  }

}
